//
//  WecomeViewController.swift
//  MicroBlog
//
//  Created by  NorthCity on 15/11/8.
//  Copyright © 2015年  NorthCity. All rights reserved.
//

import UIKit
import SDWebImage
class WecomeViewController: UIViewController {
//MARK: - 懒加载控件
/// 背景图片
    private lazy var backImageView:UIImageView = UIImageView(imageName: "ad_background")
/// 用户头像
    private lazy var iconImageView:UIImageView = {
        let iv = UIImageView(imageName: "avatar_default_big")
        iv.layer.cornerRadius = 45
        iv.layer.masksToBounds = true
        return iv
    }()
/// 欢迎标签
    private lazy var wecomeLabel:UILabel = UILabel(title: "欢迎归来", fontSize: 17)
    
    override func loadView() {
        view = backImageView
        setUpUI()
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        iconImageView.sd_setImageWithURL(UserAccountViewModel.sharedUserAccount.avatarUrl,placeholderImage: UIImage(named: "avatar_default_big"))
    }
    
//MARK: -  代理方法
    override func viewDidAppear(animated: Bool) {
//        注意不要忽略父类方法
        super.viewDidAppear(animated)
        wecomeLabel.alpha = 0
        iconImageView.snp_updateConstraints { (make) -> Void in
            make.bottom.equalTo(view.snp_bottom).offset(-view.bounds.height + 200)
        }
        
//        动画效果
            UIView.animateWithDuration(1.2, delay: 0, options: [], animations: { () -> Void in
                self.view.layoutIfNeeded()
                }){ (_) -> Void in
                 UIView.animateWithDuration(0.8, animations: { () -> Void in
                    self.wecomeLabel.alpha =  1.0
                    }, completion: { (_) -> Void in
                        NSNotificationCenter.defaultCenter().postNotificationName("TcgSwitchRootViewControllerNotification", object: nil)
                 })
            }

        
            
        }
    }

  extension WecomeViewController{
//    MARK: - 搭建界面
    private func setUpUI(){
//        1 添加控件
        view.addSubview(iconImageView)
        view.addSubview(wecomeLabel)
        
//        设置约束
        iconImageView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(view.snp_centerX)
            make.bottom.equalTo(view.snp_bottom).offset(-200)
            make.width.equalTo(90)
            make.height.equalTo(90)
        }
        
        wecomeLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(view.snp_centerX)
            make.top.equalTo(iconImageView.snp_bottom).offset(16)
        }
        
    }
}
